package com.wcpdoc.exam.api.controller;

import com.wcpdoc.core.controller.BaseController;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.entity.PageResult;
import com.wcpdoc.core.entity.PageResultEx;
import com.wcpdoc.exam.core.cache.QuestionCache;
import com.wcpdoc.exam.core.entity.Question;
import com.wcpdoc.exam.core.entity.QuestionAnswer;
import com.wcpdoc.exam.core.entity.QuestionOption;
import com.wcpdoc.exam.core.service.ExamService;
import com.wcpdoc.exam.core.util.ExamStateUtils;
import com.wcpdoc.exam.core.util.QuestionUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("api/search")
public class ApiSearchController  extends BaseController {

    @Resource
    private ExamService examService;

    /**
     * 试题搜索
     * @return
     */
    @RequestMapping("/searchQuestion")
    @ResponseBody
    public PageResult searchQuestion() {
        try {
            PageIn pageIn = new PageIn(request);
            PageOut pageOut = examService.searchQuestion(pageIn);
            return PageResultEx.ok().data(pageOut);
        } catch (Exception e) {
            return PageResult.err();
        }
    }

    /**
     * 试题搜索返回题库
     * @return
     */
    @RequestMapping("/searchQuestionType")
    @ResponseBody
    public PageResult searchQuestionType() {
        try {
            PageIn pageIn = new PageIn(request);
            return PageResultEx.ok().data(examService.searchQuestionType(pageIn));
        } catch (Exception e) {
            return PageResult.err();
        }
    }

    /**
     * 首页统计
     * @return
     */
    @RequestMapping("/homeStat")
    @ResponseBody
    public PageResult homeStat(){
        return examService.homeStat(getCurUser().getId());
    }

    /**
     * 获取某道试题
     * @param questionId
     * @return
     */
    @RequestMapping("/getQuestion")
    @ResponseBody
    public PageResult getQuestion(Integer questionId) {
        Map<String, Object> questionMap = new HashMap<>();
        Question question = QuestionCache.getQuestion(questionId);
        if (question == null) {
            return PageResult.err().msg("试题不存在");
        }
        if(examService.hasYwZsKsNow()){
            return PageResult.err().msg("有正在进行的考试，请稍后再试！");
        }



        questionMap.put("id", question.getId());
        questionMap.put("type", question.getType());
        questionMap.put("markType", question.getMarkType());
        questionMap.put("title", question.getTitle());
        questionMap.put("markOptions", question.getMarkOptions());
        questionMap.put("score", question.getScore());
        questionMap.put("analysis", question.getAnalysis());
        {// 试题选项
            List<String> options = new ArrayList<>();
            if (QuestionUtil.hasSingleChoice(question) || QuestionUtil.hasMultipleChoice(question)) {
                List<QuestionOption> questionOptionList = QuestionCache.getOption(question.getId());
                for (QuestionOption questionOption : questionOptionList) {
                    options.add(questionOption.getOptions());
                }
            }
            questionMap.put("options", options);
        }
        {// 试题答案
            List<QuestionAnswer> questionAnswerList = QuestionCache.getAnswer(question.getId());
            List<Object> answers = new ArrayList<>();
            List<BigDecimal> scores = new ArrayList<>();
            for (QuestionAnswer answer : questionAnswerList) {
                if (QuestionUtil.hasSingleChoice(question)
                        || QuestionUtil.hasTrueFalse(question)
                        || (QuestionUtil.hasQA(question) && QuestionUtil.hasSubjective(question))) {
                    answers.add(answer.getAnswer());
                } else if (QuestionUtil.hasMultipleChoice(question)) {
                    Collections.addAll(answers, answer.getAnswer().split(","));
                    scores.add(answer.getScore());
                } else if (QuestionUtil.hasFillBlank(question)
                        || (QuestionUtil.hasQA(question) && QuestionUtil.hasObjective(question))) {
                    answers.add(answer.getAnswer());
                    scores.add(answer.getScore());
                }
            }
            questionMap.put("answers", answers);
            questionMap.put("scores", scores);
        }
        return PageResultEx.ok().data(questionMap);
    }
}
